package com.egao.api.controller.nont;


import cn.hutool.core.util.StrUtil;
import com.egao.api.base.BaseApiController;
import com.egao.common.core.constants.CacheConst;
import com.egao.common.core.constants.UserConstant;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.OperRecord;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.OperRecordService;
import com.egao.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 */
@RequestMapping("/api/nt/user")
@RestController
@Api(value = "用户API",tags = {"用户API"})
public class ApiUserNtController extends BaseApiController {

    @Autowired
    private UserService userService;
    @Autowired
    private OperRecordService operRecordService;

    /**
     * 密码重置
     */
    @ApiOperation(value = "密码重置",notes = "" +
            "phoneNumber: 手机号码 必填\n" +
            "newPsw 新密码 必填\n" +
            "random 验证码 必填\n" +
            "",httpMethod = "POST")
    @PostMapping("/psw/reset")
    @ResponseBody
    public JsonResult reset(String phoneNumber, String newPsw, String random, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(UserConstant.ADMIN_ID, "密码修改", "密码修改",
                request.getServletPath(), "POST", "ApiLoginController.registerWechart", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        if (StrUtil.hasBlank( phoneNumber, newPsw, random)) {
            return JsonResult.error("参数不能为空");
        }

        User user = new User();
        user.setPhone(phoneNumber);

        try {

            user = userService.getUserByPhone(user);
        } catch (Exception e) {
            return JsonResult.error("手机号有误");
        }
        user.setPassword(userService.encodePsw(newPsw));
//        HttpSession session = request.getSession(true);
//        String random2 = (String)session.getAttribute(user.getPhone());
        // 1. 创建缓存管理器
//        CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache/ehcache-shiro.xml");
        CacheManager cacheManager = CacheManager.create("d:/ehcache/ehcache-shiro.xml");
        // 2. 获取缓存对象
        Cache cache = cacheManager.getCache(CacheConst.PHONEMESSAGEVERIFY);
        // 5. 获取缓存
        Element value = cache.get(phoneNumber);
//        System.out.println(value);
//        System.out.println(value.getObjectValue());
        String random2 = (String) value.getObjectValue();

        if (!random.equals(random2)) {
            return JsonResult.error("验证码不正确");
        }
        return userService.updatePsw(user, user);
    }

}
